﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcTasks.Models;
using System.ComponentModel;

namespace MvcTasks.ModelView.Tasks
{
    public class TasksModelView : ITasksModelView
    {
        public List<MvcTasks.Models.RandomTask> TasksList { get; set; }
        private IDbContextWrapper db;

        public TasksModelView()
        {
            db = new DbContextWrapper();
            using (db.Open())
            {
                GenreList = db.GenreSet.ToList();
            }

        }

        public TasksModelView(IDbContextWrapper dbContextWrapper)
        {
            db = dbContextWrapper;
        }
        string Title { get; set; }

        public List<Models.RandomTask> Index
        {
            get
            {
                using (db.Open())
                {
                    var tasks = db.RandomTask.ToList();
                    tasks.ForEach(db.Detach);
                    return tasks;
                }
                    
            }
            
        }
       
        public RandomTask Find(object id)
        {
            using (db.Open())
            {
                CurrentItem = db.RandomTask.Find(id);
                Name = CurrentItem.Title;
                return CurrentItem;
            }
        }

        public string Name { get; set; }

        RandomTask _currentItem;
        public RandomTask CurrentItem
        {
            get
            {
                return _currentItem;
            }
            set{
                _currentItem = value;
            }
        }

        public virtual Genre ItemGenre { get; set; }

        [DisplayName("Genre Category")]
        public virtual ICollection<Genre> GenreList { get; set; }
    }
}